#lang racket

(define (remove-it elt lst)
  (let 
      ((fil (lambda x (begin (equal? (car x) elt)))))
    (let
       ((comb (lambda (x lst) (begin (if (fil x) lst (cons x lst))))))
    (begin (foldr  comb '() lst)))))

(remove-it 5 '(1 2 3 5))

;(display x) (newline) (display lst) (newline) (display (fil x)) (newline) 

;(define (sortem lst)
;  (let 
;      ((max (lambda lst (if (empty? lst) 0 (let((cur_max (max (rest lst)))) (if (> cur_max (first lst)) cur_max (first lst)))))))
;  (if (null? lst) '() (let ((max_lst (max lst)))cons(max_lst (remove-it max_lst lst))))))

(define (sortem lst)
  (let ((maxe (lambda (x elt) (begin   (max x elt)))))
  (let 
      ((maxl (lambda (lst) (begin   (foldr maxe 0 lst)))))
  (if (null? lst) '() (let ((max_lst (begin  (maxl lst))))(begin (cons max_lst (sortem (remove-it max_lst lst)))))))))

(sortem '(1 8 3 5))


(define (sortem-gen lst test basecase)
  (let ((maxe (lambda (x elt) (begin   (test x elt)))))
  (let 
      ((maxl (lambda (lst) (begin   (foldr maxe basecase lst)))))
  (if (null? lst) '() (let ((max_lst (begin  (maxl lst))))(begin (cons max_lst (sortem-gen (remove-it max_lst lst) test basecase))))))))

(sortem-gen
  (list 0 15 4 0 8 12 3 5 1 17)
  min
  1000)